const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        auth:0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that=this;
        var open=wx.getStorageSync('open');
        wx.showShareMenu({
            menus: ['shareAppMessage', 'shareTimeline'],
            success(res) {
              console.log(res)
            },
            fail(e) {
              console.log(e)
            }
          })             
        that.setData({
            platform:app.globalData.platform,    
            open:open,
            width:app.globalData.windowWidth,
            height:app.globalData.windowHeight,
            statusBarHeight:app.globalData.statusBarHeight,
            navHeight:app.globalData.navHeight,
            marginTop:app.globalData.navTitleTop,
            domain:app.globalData.domain,
            uploadUrl:app.globalData.uploadUrl,
          })
    },
    goBack(){
        wx.navigateBack();
      },
    goto:function(e){
        console.log(e);
        var url=e.currentTarget.dataset.url;
        var parent_id=wx.getStorageSync('parent_id');
        if(url=="#"){

        }else if(url=='phone'){
            wx.makePhoneCall({
              phoneNumber: '400-800-2177',
            })
        }else{
            wx.navigateTo({
              url: url,
            })
        }
        console.log(url);
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let that=this;
      var open=wx.getStorageSync('open');
      console.log(open);
      var auth=open.auth;     
        wx.request({
            url: app.globalData.domain+'wechat/auth/getIndex',
            data:{user_id:open.id,from:1},
            success:function(res){
              console.log(res);          
                var data=res.data;
                console.log(data)
                  that.setData({
                    shareTitle:data.shareTitle,
                    shareLogo:data.shareLogo,
                    user:data.user
                   })
              
        
            }
          })
         
     
    },
    goBack(){
        wx.navigateBack();
      },
    goto:function(e){
        console.log(e);
        var url=e.currentTarget.dataset.url;
        var parent_id=wx.getStorageSync('parent_id');
        if(url=="#"){

        }else if(url=='phone'){
            wx.makePhoneCall({
              phoneNumber: '400-800-2177',
            })
        }else{
            wx.navigateTo({
              url: url,
            })
        }
        console.log(url);
    },
    uploadCard:function(e){ 
        let that=this;
        let imageType='idcard';
       
        if(this.data.user.auth == 1){
          wx.showToast({
            title: '已通过不能修改',
            icon:"error"
          })
          return false;
        }
        wx.chooseMedia({
          count: 1,
          mediaType: ['image'],
          sizeType:["compressed"],
          sourceType:["camera","album"],
          success(res){
              console.log(res);
            let tempFilePaths=res.tempFiles[0].tempFilePath;
            that.upload(imageType,tempFilePaths)
          }
        })
      
      
      },
      upload:function(imageType,path){
        wx.showToast({
          icon:"loading",
          title: '正在上传',
        })
        let that=this;        
      var timestamp=new Date().getTime(); 
      var user=this.data.user;
      console.log(path)

        wx.uploadFile({
         filePath:path ,
          name: 'file',
          url: app.globalData.domain+'wechat/auth/upload',
          formData:{
            imageType:imageType,
            },
            header: { 
              'content-type':'application/x-www-form-urlencoded'       
             },       
          success(res){
            console.log(res);
            var data=JSON.parse(res.data);
           console.log(data);
            if(data.error==0){              
              //console.log(imageType)
              if(imageType=='idcard'){
                  var word=data.info.words_result;
                
                  console.log(word);
                  var idcard = word.公民身份号码;
                  var name =  word.姓名; 
                  console.log(name);
                  var username=name.words;
                  var idcards=idcard.words;
                  console.log(username);
                  console.log(idcards);
                  user.idcard_image=data.path;
                  that.setData({
                     idcard_image:data.path,
                      name:username,
                      idcard:idcards,
                      user:user
                  })  
             
              }
       
            }
          }
      
        })
      },
      cardAuth:function(){
        let that=this; 
        let data=this.data;
        wx.request({
            url: app.globalData.domain+'wechat/auth/auth',
            data:{idcard_image:data.idcard_image,idcard:data.idcard,name:data.name,user_id:data.user.id},
            success(res){
                //console.log(res);
                var data=res.data;
                if(data.code==200){
                    wx.setStorageSync('open', data.user);
                    that.setData({
                        user:data.user
                    })
                    wx.showToast({
                      title: data.msg,
                    })
                }
            }
        }) 
      },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})